package com.cn.mmd.controller;

import com.cn.mmd.model.IntegralManage;
import com.cn.mmd.model.Student;
import com.cn.mmd.service.LogManageService;
import com.cn.mmd.service.ManageIntegralService;
import com.cn.mmd.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2016/12/14.
 */
@Controller
@RequestMapping(value = "page/manageIntegral")
public class ManageIntegralController {

    @Autowired
    private ManageIntegralService manageIntegralService;

    @Autowired
    private LogManageService logManageService;

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/add")
    public synchronized
    @ResponseBody
    Object manageIntegralAdd(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String changeAddress = request.getParameter("changeAddress");
        String changePrizeEndTime = request.getParameter("changePrizeEndTime");
        String manageId = request.getParameter("manageId");
        int num = 0;
        int n = 0;
        IntegralManage integralManage = new IntegralManage();
        List<IntegralManage> integralManageList = manageIntegralService.selectByAll(integralManage);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(changePrizeEndTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long timeStemp = date.getTime();
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        changePrizeEndTime = simpleDateFormat.format(timeStemp);
        if (integralManageList != null && integralManageList.size() == 0) {
            try {
                date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(changePrizeEndTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            long changePrizeEndTime1 = date.getTime() / 1000;
            Date date1 = new Date();
            long changePrizeEndTime2 = date1.getTime() / 1000;
            if (changePrizeEndTime1 > changePrizeEndTime2) {
                num = 1;
                n = 1;
            }else {
                objectMap.put("code", 203);
                objectMap.put("message", "时间请不要乱选择");
            }
        } else {
            try {
                date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(changePrizeEndTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            long changePrizeEndTime1 = date.getTime() / 1000;
            Date date1 = new Date();
            long changePrizeEndTime2 = date1.getTime() / 1000;
            if (changePrizeEndTime1 > changePrizeEndTime2) {
                String time = integralManageList.get(integralManageList.size() - 1).getChangeprizeendtime();
                try {
                    date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                long time1 = date.getTime() / 1000;
                if (changePrizeEndTime2 > time1) {
                    num = 1;
                    n =  integralManageList.get(integralManageList.size() - 1).getChangetopnum()+ 1;
                } else {
                    objectMap.put("code", 202);
                    objectMap.put("message", "前一期还没结束");
                }
            } else {
                objectMap.put("code", 203);
                objectMap.put("message", "时间请不要乱选择");
            }
        }
        if (num == 1) {
            Student student = new Student();
            student.setStudentremarks("离开");
            int i = studentService.countLimitNum(student);
            if (i > 5000) {
                if (i % 5000 > 3000) {
                    i = (i / 5000) + 1;
                } else {
                    i = (i / 5000);
                }
            } else {
                i = 1;
            }
            IntegralManage integralManage1 = new IntegralManage();//给本期抽奖录入数据
            integralManage1.setChangeprizeendtime(changePrizeEndTime);
            integralManage1.setChangeaddress(changeAddress);
            integralManage1.setChangetime("每个月的最后一天");
            integralManage1.setChangetopnum(n);
            integralManage1.setFirstprizenum(1 * i);
            integralManage1.setSecondprizenum(3 * i);
            integralManage1.setThirdprizenum(10 * i);
            integralManage1.setFourthprizenum(30 * i);
            integralManage1.setFifthprizenum(150 * i);
            if (manageIntegralService.insertSelective(integralManage1) > 0) {
                objectMap.put("code", 200);
                objectMap.put("message", "成功");
            } else {
                objectMap.put("code", 201);
                objectMap.put("message", "添加失败");
            }
        }
        return objectMap;
    }

    @RequestMapping(value = "/add")
    public synchronized
    @ResponseBody
    Object manageIntegralSelect(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        Map<String,Object> map ;
        return objectMap;
    }
}
